home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / MacPNG Library 1.02 / pngMacSrc 1.02 / ReadPNG.c < prev   
C/C++ Source or Header  |  1995-10-26  |  5KB  |  195 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "png.h"
  4.  
  5. // Requires: libpng, zlib, ANSI (4i) C lib's
  6.  
  7. #include <Files.h>
  8. #include <Memory.h>
  9.  
  10. PicHandle ReadPGN(FSSpec *sfFilePtr)
  11.  
  12. {    OSErr err = noErr;
  13.     short savedVol;
  14.     Str31 name;
  15.     
  16.     png_struct read_ptr;
  17.     png_info info_ptr;
  18.     png_info end_info;
  19.     FILE *fpin;
  20.     png_byte *row_buf;
  21.     png_uint_32 rowbytes;
  22.     png_uint_32 y, x;
  23.     int channels, num_pass, pass;
  24.     PicHandle myPic =  nil;
  25.         
  26.     
  27.     BlockMoveData((Ptr)(sfFilePtr.name), (Ptr)name, sizeof(Str31));
  28.     
  29.     err = GetVol(0, &savedVol);
  30.     if (err == noErr)
  31.         {
  32.             err = HSetVol(0, sfFilePtr->vRefNum, sfFilePtr->parID);
  33.             if (err == noErr)
  34.             {
  35.                 p2cstr(name);
  36.                 
  37.                 row_buf = (png_byte *)0;
  38.  
  39.                 fpin = fopen((char *) name, "rb");
  40.                 if (!fpin) {
  41.                     //    fprintf(STDERR, "Could not find input file %s\n", inname);
  42.                     return -1;
  43.                     }
  44.  
  45.                 if (setjmp(read_ptr.jmpbuf)) {
  46.                     //    fprintf(STDERR, "libpng read error\n");
  47.                     fclose(fpin);
  48.                     return -2;
  49.                     }
  50.  
  51.                 png_read_init(&read_ptr);
  52.                 png_info_init(&info_ptr);
  53.             
  54.                 png_init_io(&read_ptr, fpin);
  55.                 
  56.                 png_read_info(&read_ptr, &info_ptr);
  57.                 
  58.             #if OutputInfo    /* RMF */
  59.                 fprintf(STDERR, "%s, Width = %d, Height = %d, Depth = %d\n",
  60.                     inname, info_ptr.width, info_ptr.height, info_ptr.bit_depth);
  61.                 fprintf(STDERR, "Color type = %d, interlace_type = %d\n",
  62.                     info_ptr.color_type, info_ptr.interlace_type);
  63.             #endif
  64.             
  65.                 if ((info_ptr.color_type & 3) == PNG_COLOR_MASK_COLOR)
  66.                         channels = 3;
  67.                 else    channels = 1;
  68.                 
  69.                 if (info_ptr.color_type & PNG_COLOR_MASK_ALPHA)
  70.                     channels++;
  71.  
  72.                 rowbytes = ((info_ptr.width * info_ptr.bit_depth * channels + 7) >> 3);
  73.                 row_buf = (png_byte *)malloc((size_t)rowbytes);
  74.                 if (!row_buf) {
  75.                     fprintf(STDERR, "no memory to allocate row buffer\n");
  76.                     png_read_destroy(&read_ptr, &info_ptr, (png_info *)0);
  77.                     fclose(fpin);
  78.                     return -3;
  79.                 }
  80.             
  81.                 if (info_ptr.interlace_type) {
  82.                     num_pass = png_set_interlace_handling(&read_ptr);
  83.                 } else {
  84.                     num_pass = 1;
  85.                 }
  86.             
  87.                 
  88.                 for (pass = 0; pass < num_pass; pass++)
  89.                 {
  90.                     for (y = 0; y < info_ptr.height; y++)
  91.                     {
  92.                         png_read_rows(&read_ptr, &row_buf, (png_byte **)0, 1);    // Read one row of image data
  93.                     }
  94.                 }
  95.             
  96.                 png_read_end(&read_ptr, &end_info);
  97.                 png_read_destroy(&read_ptr, &info_ptr, &end_info);
  98.                 
  99.                 fclose(fpin);
  100.  
  101.     x = info_ptr.width;
  102.     y = info_ptr.height;
  103.     
  104.     if (info_ptr.bit_depth == 24) {    
  105.         rowbytes = (x * 4L);                    // Make Multiple of 4 bytes (32bit RGB image)
  106.     } else {
  107.         
  108.         ByteScanLine = (((x * (unsigned long) info_ptr.bit_depth) + 7L) / 8L);        /* Make Multiple of byte */
  109.         }
  110.         
  111. // info_ptr.color_type =
  112. // PNG_COLOR_TYPE_PALETTE
  113. // PNG_COLOR_TYPE_RGB
  114. // PNG_COLOR_TYPE_GRAY
  115. // PNG_COLOR_TYPE_RGB_ALPHA
  116. // PNG_COLOR_TYPE_GRAY_ALPHA
  117.     
  118. // Black & White Image
  119.  
  120.     if (info_ptr.bit_depth == 1) {                            
  121.         BitMap    theBits;      
  122.   
  123.         theBits.baseAddr = (Ptr) row_buf;            // Set up new BitMap
  124.         theBits.rowBytes = rowbytes;            // width of image
  125.         SetRect(&(theBits.bounds),     0, 0, x, y);
  126.         
  127.         myPic = MakePictFromBW(&theBits);            // create a PicHandle from B&W BitMap
  128.         
  129.                                                     // Cleanup and Exit.
  130.     } else {
  131.     
  132.         CTabHandle    macCtabHandle = nil;// Color Table for 2, 4, 8 bit images
  133.         PixMap    srcBits;
  134.         
  135.         
  136.         if (info_ptr.bit_depth >= 2 && info_ptr.bit_depth <= 8) {
  137.                 if (info_ptr.valid & PNG_INFO_PLTE) {
  138.                     info_ptr.palette;
  139.                     info_ptr.num_palette;
  140.                     macCtabHandle = nil;    // Get the color table...
  141.                     }
  142.                 }
  143.  
  144. // See: QuickDraw.h
  145. // Setup PixMap data structure:
  146.     srcBits.baseAddr = row_buf;                        // ptr to the Pixcell data
  147.                                                     // offset to next scan line
  148.     srcBits.rowBytes = rowbytes | 0x8000;        // pixmap (4, 8, 16 or 24/32bit image)
  149.  
  150.     srcBits.bounds.top = 0;                            // encloses bitmap
  151.     srcBits.bounds.left = 0;
  152.     srcBits.bounds.bottom = y;
  153.     srcBits.bounds.right = x;
  154.     srcBits.pmVersion = 0;                            // pixMap version number
  155.     
  156.     srcBits.packType = 1;                            // defines packing format: 1 = direct
  157.     
  158.     srcBits.packSize = 0L;                            // length of pixel data
  159.     srcBits.hRes = 0x480000;                        // 72dpi fixed point
  160.     srcBits.vRes = 0x480000;                        // vert. resolution (ppi)
  161.     srcBits.planeBytes = 0L;                        // offset to next plane
  162.         
  163.     srcBits.pmTable = macCtabHandle;                // color map for this pixMap
  164.     srcBits.pmReserved = 0;
  165.  
  166.     if (info_ptr.bit_depth == 24) {                            // RGB data 24bit color, no Color Table
  167.         srcBits.pixelType = RGBDirect;    
  168.         srcBits.pixelSize = 32;                        // # bits in pixel
  169.         srcBits.cmpCount = 3;                        // # components in pixel (R,G,B)
  170.         srcBits.cmpSize = 8;                        // # bits per component (3 * 8) = 
  171.                                                     //   24bits + 8bits pad = 32 bits
  172.  
  173.     } else {                                                    
  174.  
  175.         // Else: 4bit or 8bit color image
  176.         srcBits.pixelType = 0; 
  177.         srcBits.pixelSize = info_ptr.bit_depth;        // # bits in pixel
  178.         srcBits.cmpCount = 1;                        // # components in pixel
  179.         srcBits.cmpSize = info_ptr.bit_depth;        // # bits per component
  180.         }
  181.  
  182.                 
  183.         myPic = CreatePICT2( &srcBits, &srcBits.bounds, &srcBits.bounds, ditherCopy);
  184.  
  185.         if (srcBits.pmTable)    DisposHandle((Handle) srcBits.pmTable);    // Free Color table allocated
  186.                 
  187.                 
  188.     }
  189.     
  190.         /*------------ */
  191.             
  192.     free(row_buf);    
  193.     SetVol(0, savedVol);
  194.     return myPic;        
  195. }    // End readPNG()